package com.theydream.ichingwisdom.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import com.theydream.ichingwisdom.R;

public class DivinationResult extends Activity{

	private int num1;
	private ImageView card;
	private ImageView taichi;
	private ImageView cardback;
	private Animation anim;
	private boolean isFirstImage = true;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.divinationresult);
		Bundle bData = this.getIntent().getExtras();
		num1 = bData.getInt("randomNumber");

		//Set taichi
		taichi = (ImageView)findViewById(R.id.taichi);
		taichi.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Animation taichirotate = AnimationUtils.loadAnimation(DivinationResult.this, R.anim.taichirotate);
				taichirotate.setAnimationListener(new AnimationListener() {         
					public void onAnimationStart(final Animation arg0) {
						taichi.setOnClickListener(null);
					}           
					public void onAnimationRepeat(final Animation arg0) {}          
					public void onAnimationEnd(final Animation rg0){
						taichi.setVisibility(View.GONE);
						cardback.startAnimation(anim);
					}
				});
				taichi.startAnimation(taichirotate);
			}
		});
		
		//Set card back
		cardback = (ImageView) findViewById(R.id.cardBack);
		cardback.setVisibility(View.INVISIBLE);
		
		//Set card
		String uri = "drawable/card" + num1;
		int imageResource = getResources().getIdentifier(uri, null,
				getPackageName());
		card = (ImageView) findViewById(R.id.cardView);
		card.setVisibility(View.INVISIBLE);
		Drawable image = getResources().getDrawable(imageResource);
		card.setImageDrawable(image);

		//Set animation
		anim = AnimationUtils.loadAnimation(this, R.anim.cardanim);
		anim.setAnimationListener(new AnimationListener() {         
			public void onAnimationStart(final Animation arg0) {}           
			public void onAnimationRepeat(final Animation arg0) {}          
			public void onAnimationEnd(final Animation rg0){
				cardback.setVisibility(View.VISIBLE);
				cardback.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						if (isFirstImage) {       
							applyRotation(0, 90);
							isFirstImage = !isFirstImage;
						}else {    
							GlobalData.setShowCard(num1-1);
							Intent intent=new Intent(DivinationResult.this,CardDetail.class);
							startActivity(intent);
						    }
					}
				});
			}
		});
	}
	private void applyRotation(float start, float end) {
		// Find the center of image
		final float centerX = cardback.getWidth() / 2.0f;
		final float centerY = cardback.getHeight() / 2.0f;

		// Create a new 3D rotation with the supplied parameter
		// The animation listener is used to trigger the next animation
		final Flip3dAnimation rotation =
				new Flip3dAnimation(start, end, centerX, centerY);
		rotation.setDuration(500);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		rotation.setAnimationListener(new DisplayNextView(isFirstImage, cardback, card));

		if (isFirstImage)
		{
			cardback.startAnimation(rotation);
		} else {
			card.startAnimation(rotation);
		}

	}

}
